How do you configure the database connection string in the web.config file?
How do you configure the database connection string in the web.config file?
26411-Jun-2023
Updated on 14-Jun-2023
Home / DeveloperSection / Forums / How do you configure the database connection string in the web.config file?
How do you configure the database connection string in the web.config file?
Aryan Kumar
14-Jun-2023To configure the database connection string in the web.config file, you need to add a connectionStrings section to the file. The connectionStrings section is a collection of connection strings that are used by your application. Each connection string is defined by a <add> element. The <add> element has the following attributes:
Here is an example of a connectionStrings section in a web.config file:
<connectionStrings> <add name="MyConnectionString" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings>
Once you have added a connectionStrings section to the web.config file, you can reference the connection string in your application code by using the ConfigurationManager class. The ConfigurationManager class provides a method called ConnectionStrings that returns a collection of connection strings. You can then use the collection to get the connection string that you need.
Here is an example of how to get a connection string from the web.config file in ASP.NET MVC:
Code snippet